<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Screen extends MY_Controller {
    public function index(){
        $this->manage();
    }
    
    public function all(){
        $all['screens'] = $this->get_screen();
        $all_screens    = $this->load->view('screen/display_all_screens',$all);
        return $all_screens;
    }
    
    public function manage(){
        $data  = $this->load->view('screen/add_screen');
        $data .= $this->all();
        
        $system = new SYSTEM();
        $system->container($data);
    }
    
    
    public function validate(){
         $validate = array('txtAlias' => array('ALIAS'=>'Screen Alias','REQUIRED'),
                           'txtAddress' => array('ALIAS'=>'Address', 'REQUIRED'),
                           'txtCity' => array('ALIAS'=>'City', 'REQUIRED'),
                           'txtState' => array('ALIAS'=>'State', 'REQUIRED'),
                           );
         
         $form   = new FORM($validate);
         $errors = $form->fetchAll_error();
         
         if(empty($errors)){
             $capacity = ($_POST['txtAdCapacity']!='')?$_POST['txtAdCapacity']:20;
             $capacity_max = ($_POST['txtAdCapacityMax']!='')?$_POST['txtAdCapacityMax']:30;
             $size = ($_POST['txtSize']!='')?$_POST['txtSize']:0;
             $status = ($_POST['txtStatus']!='')?$_POST['txtStatus']:'ACTIVE';
             
             
               $data_input = array( 'alias'=>$_POST['txtAlias'],
                                    'address'=>$_POST['txtAddress'],
                                    'city'=>$_POST['txtCity'],
                                    'state'=>$_POST['txtState'],
                                    'postal_code'=>$_POST['txtPostal'],
                                    'ad_capacity'=>$capacity,
                                    'ad_capacity_max'=>$capacity_max,
                                    'size'=>$size,
                                    'status'=>$status,
                                    'date_added'=>date('Y-m-d H:i:s')
                                  );
               
               if($_POST['btnSubmit'] == 'Add Screen'){
                    $this->add_screen($data_input);
               }
               else if ($_POST['btnSubmit'] == 'Edit Screen'){
                  $this->edit_screen($data_input, array('id'=>$_POST['id']));
                  redirect('screen/manage');
               }
               redirect('screen/manage');
             }
          else
             {
              $data  = $this->load->view('screen/add_screen',$errors);
              $data .= $this->all();
              
              $system = new SYSTEM();
              $system->container($data);
              
             }
             
       }
       
        private function add_screen($data){
            $this->load->model('model_screen');
            $result = $this->model_screen->add_screen($data);
            return $result;
        }
        
        private function edit_screen($data,$key){
            $this->load->model('model_screen');
            $result = $this->model_screen->edit_screen($data,$key);
            return $result;
        }
    
    private function get_screen(){
        $this->load->model('model_screen');
        $result = $this->model_screen->get_all_screens();
        return $result;
    }
}


?>
